use ggez::{glam::Vec2, Context, graphics};
use crate::particle;




pub struct PraticleSystem {
    particles: Vec<particle::Particle>,
    origin: Vec2,
    rot: f32,
    is_emit: bool,
    param: graphics::DrawParam
}

impl PraticleSystem {
    pub fn new(origin: Vec2, rot: f32) -> PraticleSystem {
        let particles: Vec<particle::Particle> = vec![];
        let is_emit = false;
        let param = graphics::DrawParam::default();
        PraticleSystem { particles, origin, rot, is_emit, param}
    }

    pub fn start_emit(&mut self) -> () {
        self.is_emit = true
    }

    pub fn stop_emit(&mut self) -> () {
        self.is_emit = false
    }

    pub fn update (&mut self, origin: Vec2, rot: f32) -> () {
        let r = self.rot + rot;
        let o = Vec2::from_angle(r).rotate(self.origin) + origin;
        if self.is_emit {
            let particle = particle::Particle::new(o, r);
            self.particles.push(particle);
        }
        
        
        self.param = graphics::DrawParam::default().dest(o).rotation(r);

        
        self.particles.retain_mut(|particle| {
            particle.update();
            !particle.is_dead()
        });
    }

    pub fn display (&mut self, ctx: &mut Context, canvas : &mut graphics::Canvas) -> () {
        for particle in &mut self.particles {particle.display(ctx, canvas)}
        
    }
}